/* 
 * Cashbox is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "CBReportViewController.h"
#import "CBAccount.h"
#import <Cashbox/CBReport.h>
#import <Cashbox/CBDataControllerProtocol.h>

void *CBReportChangeContext = @"CBReportChangeContext";

@interface CBReportViewController ()
@property (retain) CBViewController *contentController;
@end

@interface CBReportViewController (PRIVATE)
- (void)setupContent;
@end

@implementation CBReportViewController

+ (NSString *)nibName {
	return @"Report";
}

- (id)init {
	if ((self = [super init])) {
		[self addObserver:self
			   forKeyPath:@"report"
				  options:0
				  context:CBReportChangeContext];
	}
	return self;
}

- (void)dealloc {
	[report release];
	[contentController release];
	[super dealloc];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
					  ofObject:(id)object
						change:(NSDictionary *)change
					   context:(void *)context {
	if (context == CBReportChangeContext)
	{
		if ([self isViewLoaded]) {
			[self setupContent];
		}
	}
}

- (void)awakeFromNib {
	[self setupContent];
}

- (void)setupContent {
	if ([self report]) {
		Class controllerClass = [[[self report] class] viewControllerClass];
		if (controllerClass != [[self contentController] class]) {
			// remove any previous view
			while ([[[self view] subviews] count]) {
				[[[[self view] subviews] objectAtIndex:0] removeFromSuperview];
			}
			
			[self setContentController:[[[controllerClass alloc] init] autorelease]];
			
			NSView *contentView = [[self contentController] view];
			NSRect frame = [[self view] frame];
			frame.origin.x = 0;
			frame.origin.y = 0;
			[contentView setFrame:frame];
			[[self view] addSubview:contentView];
		}
		
		[(id)[self contentController] setReport:[self report]];
	}
}


#pragma mark properties
// ----------------------------------------------------------------------------------------------------
// properties
// ----------------------------------------------------------------------------------------------------

@synthesize report;
@synthesize contentController;


#pragma mark autosavename
// ----------------------------------------------------------------------------------------------------
// autosavename
// ----------------------------------------------------------------------------------------------------

- (void)setAutosaveName:(NSString *)name {
	// nothing to save
}

- (NSString *)autosaveName {
	// nothing saved
	return nil;
}


@end
